package utils

import (
	"os"
	"path"
	"strconv"
	"strings"
	"time"

	"github.com/GoAdminGroup/go-admin/modules/logger"
	"github.com/op/go-logging"
)

var execCmd = path.Base(os.Args[0])
var log = logging.MustGetLogger(execCmd)

// 辅助转换函数
func ToInt(s string) int {
	i, err := strconv.Atoi(s)
	if err != nil {
		return 0 // 返回0当类型转换失败
	}
	return i
}

func ToFloat64(s string) float64 {
	f, err := strconv.ParseFloat(s, 64)
	if err != nil {
		return 0 // 返回0当类型转换失败
	}
	return f
}

func ToString(s string) string {
	if s == "<nil>" {
		return ""
	}
	return s
}

func ToDate(s string) *time.Time {
	if s == "" {
		return &time.Time{}
	}
	t, err := time.Parse("2006-01-02", s)
	if err != nil {
		logger.Error("ToDate %s err: %+v", s, err)
	}
	return &t
}

func ToDateV2(s string) string {
	if s == "" {
		return ""
	}
	s = strings.Split(s, " ")[0]
	s = strings.Split(s, "T")[0]
	return s
}
